package com.vinaprime.primeschedule.databaselayer;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vinaprime.primeschedule.entities.VPEvent;

@SuppressLint("NewApi")
public class VPEvent_DL {
	private static final String DB_TABLE_NAME = "VP_EVENT";
	private static final String DB_TABLE_VPE_ID = "VPE_ID";
	private static final String DB_TABLE_VPE_TITLE = "VPE_TITLE";
	private static final String DB_TABLE_VPE_ADRESS = "VPE_ADDRESS";
	private static final String DB_TABLE_VPE_LOCATION = "VPE_LOCATION";
	private static final String DB_TABLE_VPE_REPEATSCHEDULE = "VPE_REPEATSCHEDULE";
	private static final String DB_TABLE_VPE_ALARM = "VPE_ALARM";
	private static final String DB_TABLE_VPE_URL = "VP_URL";
	private static final String DB_TABLE_VPE_AVAILABLE = "VP_AVAILABLE";
	private static final String DB_TABLE_VPE_DESCRIPTION = "VPE_DESCTIPTION";
	private static final String DB_TABLE_VPE_STARTDATE = "VPE_STARTDATE";
	private static final String DB_TABLE_VPE_ENDDATE= "VPE_ENDDATE";
	public DatabaseController_DL myDatabaseControllerDL;
	private SimpleDateFormat myDateformat;
	private static final String DB_dateformat = "MM/dd/yyyy";
	
	public VPEvent_DL(Context context, String path){
		this.myDatabaseControllerDL = new DatabaseController_DL(context, path);
		this.myDateformat = new SimpleDateFormat(DB_dateformat);
	}
	public DatabaseController_DL getMyDatabaseControllerDL() {
		return myDatabaseControllerDL;
	}
	public void setMyDatabaseControllerDL(
			DatabaseController_DL myDatabaseControllerDL) {
		this.myDatabaseControllerDL = myDatabaseControllerDL;
	}
	public static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	public static String getDbTableVpeId() {
		return DB_TABLE_VPE_ID;
	}
	public static String getDbTableVpeTitle() {
		return DB_TABLE_VPE_TITLE;
	}
	public static String getDbTableVpeAdress() {
		return DB_TABLE_VPE_ADRESS;
	}	
	public static String getDbTableVpeLocation() {
		return DB_TABLE_VPE_LOCATION;
	}
	public static String getDbTableVpeRepeatschedule() {
		return DB_TABLE_VPE_REPEATSCHEDULE;
	}
	public static String getDbTableVpeAlarm() {
		return DB_TABLE_VPE_ALARM;
	}
	public static String getDbTableVpeUrl() {
		return DB_TABLE_VPE_URL;
	}
	public static String getDbTableVpeAvailable() {
		return DB_TABLE_VPE_AVAILABLE;
	}
	public static String getDbTableVpeDescription() {
		return DB_TABLE_VPE_DESCRIPTION;
	}
	public static String getDbTableVpeStartdate() {
		return DB_TABLE_VPE_STARTDATE;
	}
	public static String getDbTableVpeEnddate() {
		return DB_TABLE_VPE_ENDDATE;
	}
	
	public SimpleDateFormat getMyDateformat() {
		return myDateformat;
	}
	public void setMyDateformat(SimpleDateFormat myDateformat) {
		this.myDateformat = myDateformat;
	}
	public static String getDbDateformat() {
		return DB_dateformat;
	}
	//Cac phuong thuc lien quan
	public Cursor getAllEvent()
	{
		Cursor cur = null;
		SQLiteDatabase  db = this.myDatabaseControllerDL.getWritableDatabase();
		String strSelect = "SELECT * FROM "+DB_TABLE_NAME;
		cur = db.rawQuery(strSelect, null);		
		return cur;
		
	}
	public Cursor getEventbyDate(Date myDate){
		Cursor cur = null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM " + DB_TABLE_NAME + " WHERE " + DB_TABLE_VPE_STARTDATE+ "<= "+ myDate + " AND "+ DB_TABLE_VPE_ENDDATE + " >= "+ myDate;
	    cur = db.rawQuery(strQuery,null);
	    return cur;
	}
	public Cursor getNewEventID()
	{
		Cursor cur  = null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT MAX ("+DB_TABLE_VPE_ID+") AS " +DB_TABLE_VPE_ID+ " FROM "+ DB_TABLE_NAME;
		cur  = db.rawQuery(strQuery, null);
		return cur;
	}
	public Cursor getEventbyId(int Id)
	{
		Cursor cur =  null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPE_ID +" = " + Id ;
		cur = db.rawQuery(strQuery, null);	
		return cur;
	}
		public void AddEvent (VPEvent event)	{
			SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(DB_TABLE_VPE_ID, event.getVpeId());
			values.put(DB_TABLE_VPE_TITLE, event.getVpeTitle());
			values.put(DB_TABLE_VPE_ADRESS, event.getVpeAddress());
			values.put(DB_TABLE_VPE_LOCATION, event.getVpeLocation());		
			values.put(DB_TABLE_VPE_REPEATSCHEDULE, event.getVpeRepeatSchedule());
			values.put(DB_TABLE_VPE_ALARM, event.getVpeAlarm());
			values.put(DB_TABLE_VPE_URL, event.getVpeURL());
			values.put(DB_TABLE_VPE_AVAILABLE,  event.getVpeAvailable());
			values.put(DB_TABLE_VPE_DESCRIPTION,  event.getVpeDescription());
			if (event.getVpeStartDate() != null){
				values.put(DB_TABLE_VPE_STARTDATE, myDateformat.format(event.getVpeStartDate()));
			}else{
				values.putNull(DB_TABLE_VPE_STARTDATE);
			}
			if (event.getVpeEndDate() != null){
				values.put(DB_TABLE_VPE_ENDDATE, myDateformat.format(event.getVpeEndDate()));
			}else{
				values.putNull(DB_TABLE_VPE_ENDDATE);			
				}	
			
			db.insert(DB_TABLE_NAME, null, values);		
			db.close();
		}
		public int updateEvent (VPEvent event)
		{
			SQLiteDatabase db = this.myDatabaseControllerDL .getWritableDatabase();
			ContentValues values = new ContentValues();
			values.put(DB_TABLE_VPE_ID, event.getVpeId());
			values.put(DB_TABLE_VPE_TITLE, event.getVpeTitle());
			values.put(DB_TABLE_VPE_ADRESS, event.getVpeAddress());
			values.put(DB_TABLE_VPE_LOCATION, event.getVpeLocation());		
			values.put(DB_TABLE_VPE_REPEATSCHEDULE, event.getVpeRepeatSchedule());
			values.put(DB_TABLE_VPE_ALARM, event.getVpeAlarm());
			values.put(DB_TABLE_VPE_URL, event.getVpeURL());
			values.put(DB_TABLE_VPE_AVAILABLE,  event.getVpeAvailable());
			values.put(DB_TABLE_VPE_DESCRIPTION,  event.getVpeDescription());
			if (event.getVpeStartDate() != null){
				values.put(DB_TABLE_VPE_STARTDATE, myDateformat.format(event.getVpeStartDate()));
			}else{
				values.putNull(DB_TABLE_VPE_STARTDATE);
			}
			if (event.getVpeEndDate() != null){
				values.put(DB_TABLE_VPE_ENDDATE, myDateformat.format(event.getVpeEndDate()));
			}else{
				values.putNull(DB_TABLE_VPE_ENDDATE);			
				}			
			return db.update(DB_TABLE_NAME, values, DB_TABLE_VPE_ID + "= ?", new String[] {String.valueOf(event.getVpeId())});
			
		}
		
		public void deleteEvent (VPEvent event)
		{
			SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
			db.delete(DB_TABLE_NAME, DB_TABLE_VPE_ID + "= ?", new String[] { String.valueOf(event.getVpeId()) });
			db.close();
		}
		public void deleteEventbyID(int _VpeID){
			SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
			db.delete(DB_TABLE_NAME, DB_TABLE_VPE_ID + "= ?",new String[]{ String.valueOf(_VpeID)});
			db.close();
		}
	
}

